package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StateCode;
import cn.tedu._04weibo.mapper.CommentMapper;
import cn.tedu._04weibo.pojo.dto.CommentInsertParam;
import cn.tedu._04weibo.pojo.entity.Comment;
import cn.tedu._04weibo.pojo.vo.CommentListQuery;
import cn.tedu._04weibo.pojo.vo.UserSelectQuery;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/comment/")
@Api(tags = "03.评论管理")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @ApiOperation("发表评论")
    @ApiOperationSupport(order = 10)
    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentInsertParam commentInsertParam, @ApiIgnore HttpSession session){
        UserSelectQuery query=(UserSelectQuery)session.getAttribute("user");
//        if (query==null){
//            return new JsonResult(StateCode.NOT_LOGIN);
//        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentInsertParam,comment);
        comment.setCreateTime(new Date());
        comment.setUserId(query.getId());
        commentMapper.insert(comment);
        return JsonResult.ok();
    }
    //获取评论列表
    @ApiOperation("获取评论列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("selectByWeiboId")
    public JsonResult selectByWeiboId(Integer id){
        List<CommentListQuery> commentListQueries=commentMapper.selectById(id);
        return JsonResult.ok(commentListQueries);
    }
}










